<?php 
	require('../db_connect.php');

	$sql = "SELECT * from realms";
	$realms = $db->Execute($sql);

	$sql = "SHOW COLUMNS from realms";	
	$realm_headers = $db->Execute($sql);

//var_dump($realm_headers);
$field = $realm_headers->fields[0];
//$realm_headers->MoveNext();
//echo  $realm_headers->FieldCount() . "<br/>";


$json = '{"realm":[';
$first = 1;
while (!$realms->EOF) {
	if ($first==0)
		$json = $json . ',';	
	$json .= '{';
	$realm_headers->MoveFirst();
	for($i=0;$i<$realm_headers->FieldCount()+2;$i++){
		$field = $realm_headers->fields[0];
		if(substr($field,0,6)=="realm_")		
			$json_name = substr($field,stripos($field,"_")+1);		
		else
			$json_name = $field;
		$realm_headers->MoveNext();
		$json .= '"' .$json_name. '":' . '"' .$realms->fields[$field]. '"';
		if($i != $realm_headers->FieldCount()+1)
		$json .= ',';
	}//end for loop
	$json .= '}';
	$realms->MoveNext();
	$first = 0;
}
$json = $json . ']}';

echo $json;
?>
